package org.davidliebman.android.awesomecube;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.Handler;
import android.util.Log;
import android.content.*;

public class OpenGLRenderer implements Renderer {
	//private final PieceList mPieces;
	public MeshList mPieces;
	private Context mContext;
	public GL10 mGl;
	
	public OpenGLRenderer(Context c) {
		mContext = c;
		// Initialize our root.
		
		
		mPieces = new MeshList(mContext);
	}

	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		mGl = gl;
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Replace the current matrix with the identity matrix
		gl.glLoadIdentity();
		// setup angle before drawing...
		mPieces.setDisplayAngle();
		// Draw our scene.
		mPieces.draw(gl);
		
	}


	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);
		// Reset the projection matrix
		gl.glLoadIdentity();
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				1000.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		//gl.glLoadIdentity();
	}


	public void startAnimation( Integer [][][] mTiles, UsedXYZ mUsed, int mDirection, int mAxis, int mLimit, int mStep) {
		mPieces.startAnimation( mTiles,  mUsed, mDirection, mAxis, mLimit, mStep);
	}
	
	public void setTilt(Positioning mP) {
		mPieces.setTilt(mP);
	}
	
	public void setScreenDimensions(int mScreenX, int mScreenY) {
		mPieces.mScreenX = mScreenX;
		mPieces.mScreenY = mScreenY;
	}
	
	public void setDisplayAngleFromMenu() {
		mPieces.mChildren.setViewingAngle(new Positioning());
		mPieces.mPosition = new Positioning();
		mPieces.mTilt = new Positioning();
		mPieces.mMirror.setViewingAngle(new Positioning());
		mPieces.mUseTilt = false;
		mPieces.setDisplayAngle();
	}
	
	public void setAnimate(boolean mAnimate) {
		mPieces.mUseTurningAnimation = mAnimate;
	}
	
	public void addArray(OrientationArray object) {
		mPieces.mChildren = object;
	}
	public void setUseDisplayAtAngle( boolean mUse) {
		mPieces.setUseDisplayAtAngle(mUse);
	}
	public void setPointAndHandler( PointXYZ in , Handler mHandler) {
		mPieces.mVec = in;
		//mPieces.mCamWidth = camWidth;
		//mPieces.mCamHeight = camHeight;
		if (in.x != 0 || in.y != 0) mPieces.mUsePicking = true;
		else mPieces.mUsePicking = false;
		
		mPieces.mPads.mHandler = mHandler;
	}
	public void setUseMirror( boolean mUseMirror) {
		mPieces.mUseMirror = mUseMirror;
	}
	public void setChosenMove ( int mMove ) {
		mPieces.mPads.mChosenMove = mMove;
		mPieces.mPads.mChosenArrow = -1;
	}
}
